const dateTimePicker = require('../../utils/dateTimePicker');

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    placeholder: {
      type: String,
      value: '请选择'
    },
    accuracy: {
      type: Number,
      value: 6 // 0-6:分别对应 年、月、日、时、分、秒 6级精度
    },
    interval: {
      type: Number,
      value: 1 // 当前精度下的间隔
    },
    headerText: {
      type: String,
      value: ''
    },
    // 开始年份
    startYearProp: {
      type: Number,
      value: 0
    },
    // 结束年份
    endYearProp: {
      type: Number,
      value: 0
    },
    // 排除过去
    excludePast: {
      type: Boolean,
      value: true
    },
    // 结束时间范围字符串
    rangeTarget: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    MAX_ACCURAACY: 6,
    dateTimeArray: null,
    dateTime: null,
    startYear: (new Date()).getFullYear(),
    endYear: (new Date()).getFullYear() + 1,
    value: ''
  },

  /**
   * 生命周期
   */
  attached () {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    let obj = dateTimePicker.dateTimePicker(
      this.data.startYearProp || this.data.startYear,
      this.data.endYearProp || this.data.endYear
    );

    // 处理精度
    for (let i = 0, l = this.data.MAX_ACCURAACY - this.data.accuracy; i < l; i += 1) {
      obj.dateTimeArray.pop();
    }

    obj.dateTimeArray[obj.dateTimeArray.length - 1] = obj.dateTimeArray[obj.dateTimeArray.length - 1].filter(item => {
      const num = +item;
      return num % this.data.interval === 0;
    });

    this.setValue(obj.dateTime, obj.dateTimeArray, false);
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取显示值
    getValue () {
      let str = '';
      for (let i = 0; i < this.data.accuracy; i += 1) {
        if (this.data.dateTimeArray[i] && this.data.dateTimeArray[i][this.data.dateTime[i]]) {
          str += this.data.dateTimeArray[i][this.data.dateTime[i]];
          if (i < 2) {
            str += '-';
          } else if (i === 2) {
            str += ' ';
          } else if (i < this.data.accuracy - 1) {
            str += ':';
          }
        }
      }
      return str;
      // return `${this.data.dateTimeArray[0][this.data.dateTime[0]]}-${this.data.dateTimeArray[1][this.data.dateTime[1]]}-${this.data.dateTimeArray[2][this.data.dateTime[2]]} ${this.data.dateTimeArray[3][this.data.dateTime[3]]}:${this.data.dateTimeArray[4][this.data.dateTime[4]]}:${this.data.dateTimeArray[5][this.data.dateTime[5]]}`;
    },
    // 设置显示值
    setValue (dateTime, dateTimeArray, setvalue = true) {
      const param = {};
      if (dateTime) {
        param.dateTime = dateTime;
      }
      if (dateTimeArray) {
        param.dateTimeArray = dateTimeArray;
      }
      this.setData(param, () => {
        if (setvalue) {
          this.setData({ value: this.getValue() }, () => {
            this.triggerEvent('value', { value: this.data.value });
          });
        }
      });
    },
    changeDateTime (e) {
      this.setValue(e.detail.value);
    },
    // 绑定 bindcolumnchange 事件
    changeDateTimeColumn (e) {
      let arr = this.data.dateTime; let dateArr = this.data.dateTimeArray;

      arr[e.detail.column] = e.detail.value;
      dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

      this.setValue(arr, dateArr, false);
    },
    // 取消按钮
    handleCancel () {
      this.setData({ value: '' });
      this.triggerEvent('cancel');
    }
  }
});
